﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MultipleCopyPaste
{
    public partial class Form1 : Form
    {
        private int copyCount = 1;
        private static IList<MultipleCopyPaste.BusinessEntity.ClipBoardObject> ClipBoardList = new List<MultipleCopyPaste.BusinessEntity.ClipBoardObject>();

        public Form1()
        {
            InitializeComponent();
            dataGridView1.Visible = false;
            label1.Text = "Copy content from the above textbox and paste it in the below.";
        }

        private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.C)
            {
                MultipleCopyPaste.BusinessEntity.ClipBoardObject clipBoardObject = new BusinessEntity.ClipBoardObject();
                clipBoardObject.index = copyCount;
                clipBoardObject.content = richTextBox1.SelectedText;

                ClipBoardList.Add(clipBoardObject);
                copyCount++;
            }

            if (copyCount > 9)
            {
                copyCount = 1;
                ClipBoardList = new List<MultipleCopyPaste.BusinessEntity.ClipBoardObject>();
            }
        }

        private void richTextBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.Shift)
            {
                dataGridView1.DataSource = ClipBoardList;
                dataGridView1.Visible = true;
            }

            if (e.Control && e.Shift && e.KeyCode == Keys.D1)
            {
                dataGridView1.Visible = false;
                LoadRichText(0);
            }

            if (e.Control && e.Shift && e.KeyCode == Keys.D2)
            {
                dataGridView1.Visible = false;
                LoadRichText(1);
            }

            if (e.Control && e.Shift && e.KeyCode == Keys.D3)
            {
                dataGridView1.Visible = false;
                LoadRichText(2);
            }

            if (e.Control && e.Shift && e.KeyCode == Keys.D4)
            {
                dataGridView1.Visible = false;
                LoadRichText(3);
            }

            if (e.Control && e.Shift && e.KeyCode == Keys.D5)
            {
                dataGridView1.Visible = false;
                LoadRichText(4);
            }

            if (e.Control && e.Shift && e.KeyCode == Keys.D6)
            {
                dataGridView1.Visible = false;
                LoadRichText(5);
            }

            if (e.Control && e.Shift && e.KeyCode == Keys.D7)
            {
                dataGridView1.Visible = false;
                LoadRichText(6);
            }

            if (e.Control && e.Shift && e.KeyCode == Keys.D8)
            {
                dataGridView1.Visible = false;
                LoadRichText(7);
            }

            if (e.Control && e.Shift && e.KeyCode == Keys.D9)
            {
                dataGridView1.Visible = false;
                LoadRichText(8);
            }

            if (e.Control && e.Shift && e.KeyCode == Keys.D0)
            {
                dataGridView1.Visible = false;
                LoadRichText(9);
            }
        }

        private void LoadRichText(int index)
        {
            var selectionIndex = richTextBox2.SelectionStart;

            if (index <= ClipBoardList.Count)
            {
                richTextBox2.Text = richTextBox2.Text.Insert(selectionIndex, ClipBoardList[index].content);
                richTextBox2.SelectionStart = selectionIndex + ClipBoardList[index].content.Length;
            }
        }
    }
}
